package com.example.girl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.girl.adapter.FeedManager_Adapter;
import com.example.girl.bean.Fk_list;
import com.example.girl.bean.MyEvent;
import com.example.girl.http.APIRetrofit;
import com.example.girl.http.APIService;
import com.example.lbstest.R;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.king.base.util.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class StudentManagerActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.lv_list)
    ListView lvList;
    FeedManager_Adapter member_adapter;
    @BindView(R.id.button4)
    Button button4;
    List<Fk_list.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studentmanager);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        member_adapter = new FeedManager_Adapter();
        lvList.setAdapter(member_adapter);


        updata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getSc())) {
            if (event.getSc().equals("updata")) {
                updata();
            }
        }
    }

    KProgressHUD dialog;

    public void showdialog() {
        dialog = KProgressHUD.create(StudentManagerActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在登录中，请稍候")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    private void updata() {
        showdialog();
        APIRetrofit.getInstance()
                .create(APIService.class)
                .Queryfeedback()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Fk_list>() {
                    @Override
                    public void onCompleted() {
                        LogUtils.d("onCompleted");

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e(e);
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        Toast.makeText(StudentManagerActivity.this, "" + e, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Fk_list girlResult) {
                        LogUtils.d(girlResult.toString());
                        try {
                            if (dialog != null) {
                                dialog.dismiss();
                            }
                            if (girlResult.getCode() == 10001) {


                                data = girlResult.getData();
                                member_adapter.clear();
                                if (data != null && data.size() > 0) {


                                    member_adapter.addItem(data);
                                }


                            }
                        } catch (Exception e) {

                        }
                    }

                });


    }

    @OnClick({R.id.img_back, R.id.button4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.button4:
                startActivity(new Intent(this, Feedback_Activity.class));
                break;
        }
    }
}
